package com.zhfi.dataapply.schedule.reader;

import com.alibaba.fastjson.JSON;
import com.zhfi.dataapply.entity.TzResponse;
import com.zhfi.dataapply.schedule.companylist.CompanyListService;
import com.zhfi.dataapply.service.ForwardService;
import lombok.Getter;
import org.springframework.batch.item.ItemReader;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 通过唯一表示companyId获取数据的ItemReader
 */
@Getter
public abstract class AbstractKeyReader<T> implements ItemReader<T> {

    /**
     * 持久化类型
     */
    private Class<T> clazz;

    /**
     * 企业列表中的偏移量
     */
    private int companySiteNum;

    /**
     * 数据列表读取的偏移量
     */
    private int dataSiteNum;

    /**
     * 总数据列表
     */
    private List<T> dataList;


    private ForwardService forwardService;
    private CompanyListService companyListService;

    public AbstractKeyReader(Class<T> clazz, ForwardService forwardService,
                             CompanyListService companyListService) {
        this.clazz = clazz;
        this.companySiteNum = 0;
        this.dataSiteNum = 0;
        this.dataList = Collections.emptyList();
        this.companyListService = companyListService;
        this.forwardService = forwardService;
    }

    @Override
    public T read() {
        //如果已读完企业列表中所有企业，则返回null
        if (notUpdateAll() && isDateBeyond()) {
            return null;
        }
        return readOne();
    }

    private T readOne() {
        //如果已读完企业列表中所有企业，则返回null
        if (companySiteNum >= companyListService.getSize()) {
            return null;
        }
        if (CollectionUtils.isEmpty(dataList) || dataSiteNum >= dataList.size()) {
            this.dataSiteNum = 0;
            companySiteNum ++;
            readCompanyData();
        }
        if (CollectionUtils.isEmpty(dataList)) {
            return readOne();
        }
        return dataList.get(dataSiteNum ++);
    }


    protected boolean notUpdateAll() {
        return true;
    }

    protected boolean isDateBeyond() {
        //todo
        return false;
    }

    /**
     * 重置为读取任务起始状态
     */
    public void reset() {
        this.companySiteNum = 0;
        this.dataSiteNum = 0;
        dataList = Collections.emptyList();
        resetUrl();
        readCompanyData();
    }

    /**
     * 读取企业数据放置在dataList中
     */
    private void readCompanyData() {
        if (companySiteNum >= companyListService.getSize()) {
            dataList = Collections.emptyList();
            return;
        }
        //获取企业标识companyId
        String companyId = companyListService.getCompanyId(companySiteNum);
        TzResponse tzResponse = forwardService.requestForwarder(getUrl(), getParamsMap(companyId));
        if (null == tzResponse || CollectionUtils.isEmpty(tzResponse.getData())) {
            companySiteNum ++;
            readCompanyData();
        } else {
            dataList = JSON.parseArray(JSON.toJSONString(tzResponse.getData()), clazz);
        }
    }

    /**
     * 获取参数Map
     * @param companyId 企业ID
     * @return 参数Map
     */
    protected Map<String, Object> getParamsMap(String companyId) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("companyId", companyId);
        return paramsMap;
    }

    public abstract String getUrl();
    protected abstract void resetUrl();

}
